package com.qyhl.module_practice.volunteer.newlist.newvol.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeVolunteerBean;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class PracticeVolunteerNewFragment extends BaseFragment implements PracticeVolunteerNewContract.PracticeVolunteerNewView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;


    private View view;

    private PracticeVolunteerNewPresenter mPresenter;

    private CommonAdapter<PracticeVolunteerBean> commonAdapter;

    private List<PracticeVolunteerBean> mDataList = new ArrayList<>();

    private int page = 1;

    private int type;

    public void setType(int type) {
        this.type = type;
    }

    public static PracticeVolunteerNewFragment newInstance(int type) {
        PracticeVolunteerNewFragment fragment = new PracticeVolunteerNewFragment();
        fragment.setType(type);
        return fragment;
    }

    @Override
    public void initImmersionBar() {

    }

    /**
     * 加载数据
     */
    private void loadData() {
        //下拉刷新
        initView();

    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.practice_fragment_volunteer_new, null);
    }

    @Override
    protected void create() {
        mPresenter = new PracticeVolunteerNewPresenter(this);
        loadData();
    }

    @Override
    protected void setListener() {
        setListeners();
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);

        recycleView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeVolunteerBean>(getContext(), R.layout.practice_item_volunteer_new, mDataList) {
            @Override
            protected void convert(ViewHolder holder, PracticeVolunteerBean bean, int position) {
                if (position == 0) {
                    holder.setVisible(R.id.top_tag, true);
                } else {
                    holder.setVisible(R.id.top_tag, false);
                }

                holder.setText(R.id.name, "姓名：" + bean.getName());

                if (bean.getOrgs() != null && bean.getOrgs().size() > 0) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < bean.getOrgs().size(); i++) {
                        if (i == 0) {
                            stringBuilder.append(bean.getOrgs().get(i).getName());
                        } else {
                            stringBuilder.append(",").append(bean.getOrgs().get(i).getName());
                        }

                    }
                    holder.setText(R.id.organization, "服务队：" + stringBuilder.toString());
                } else {
                    holder.setText(R.id.organization, "服务队：无");
                }

                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(PracticeVolunteerNewFragment.this)
                        .load(bean.getLogo())
                        .apply(new RequestOptions().error(R.drawable.practice_vol_default_icon).placeholder(R.drawable.practice_vol_default_icon))
                        .into(cover);

                holder.setVisible(R.id.level_tag, true);
                switch (bean.getLevel()) {
                    case 0:
                        holder.setVisible(R.id.level_tag, false);
                        break;
                    case 1:
                        holder.setImageResource(R.id.level_tag, R.drawable.practice_vol_lv1_icon);
                        break;
                    case 2:
                        holder.setImageResource(R.id.level_tag, R.drawable.practice_vol_lv2_icon);
                        break;
                    case 3:
                        holder.setImageResource(R.id.level_tag, R.drawable.practice_vol_lv3_icon);
                        break;
                    case 4:
                        holder.setImageResource(R.id.level_tag, R.drawable.practice_vol_lv4_icon);
                        break;
                    case 5:
                        holder.setImageResource(R.id.level_tag, R.drawable.practice_vol_lv5_icon);
                        break;
                }
            }
        });

        switch (type) {
            case 0:
                mPresenter.getListByAct(page + "");
                break;
            case 1:
                mPresenter.getListByTime(page + "");
                break;
            case 2:
                mPresenter.getListByScore(page + "");
                break;
        }
    }

    private void setListeners() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                page = 1;
                switch (type) {
                    case 0:
                        mPresenter.getListByAct(page + "");
                        break;
                    case 1:
                        mPresenter.getListByTime(page + "");
                        break;
                    case 2:
                        mPresenter.getListByScore(page + "");
                        break;
                }
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                switch (type) {
                    case 0:
                        mPresenter.getListByAct(page + "");
                        break;
                    case 1:
                        mPresenter.getListByTime(page + "");
                        break;
                    case 2:
                        mPresenter.getListByScore(page + "");
                        break;
                }
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                switch (type) {
                    case 0:
                        mPresenter.getListByAct(page + "");
                        break;
                    case 1:
                        mPresenter.getListByTime(page + "");
                        break;
                    case 2:
                        mPresenter.getListByScore(page + "");
                        break;
                }
            }
        });
    }


    @Override
    public void setList(List<PracticeVolunteerBean> beanList, boolean isMore) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        page++;
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(getContext())) {
                showToast("网络异常，请检查您的网络！", 4);
            } else if (msg.contains("暂无")) {
                showToast("暂无更多志愿者！", 4);
            } else {
                showToast(msg, 4);
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            loadMask.setReloadButtonText("点击重试~");
            if (!NetUtil.isNetworkAvalible(getContext())) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }
}
